package com.jjt.kiki.ip.strategy;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.jjt.kiki.bean.Ip;
import com.jjt.kiki.ip.IpCrawlerException;

public class Data5u implements IpCrawlerStrategy{

	String address = "http://www.data5u.com/";
	@Override
	public String getAddress() {
		return address;
	}

	@Override
	public boolean isBrowser() {
		return true;
	}

	@Override
	public List<Ip> analyse(String pageSource) throws IpCrawlerException {
		List<Ip> list = new ArrayList<Ip>();
		Document doc = Jsoup.parse(pageSource);
		Elements ipEs = doc.getElementsByClass("l2");
		Iterator<Element> ipEsI =   ipEs.iterator();
		while(ipEsI.hasNext()) {
			Element e = ipEsI.next();
			List<Element> childs = e.getElementsByTag("span");
			String country = childs.get(4).text();
			if (country.equals("中国")) {
				String host = childs.get(0).text();
				String port = childs.get(1).text();
				Ip ip = new Ip();
				ip.setIpHost(host);
				ip.setIpPort(Integer.parseInt(port));
				list.add(ip);
			}
		}
		return list;
	}

}
